# -*- coding: utf-8 -*-
import time

from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.extensions.android.nativekey import AndroidKey
import os

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def agree():
    driver.find_element(MobileBy.XPATH,
                        '//android.widget.TextView[@resource-id="com.ss.android.ugc.aweme:id/bdb"]').click()
    print("点击同意")
    time.sleep(5)


def swipe():
    # 滑动⻚⾯5次(有过渡)
    for i in range(5):
        driver.swipe(start_x=555, start_y=1500, end_x=555, end_y=400, duration=800)
        print("滑动第{}次".format(i + 1))
        time.sleep(2)  # 等待⻚⾯滚动完成
    print("滑动完成")


def swipe_onlyOne():
    # 模拟滑动
    driver.swipe(start_x=555, start_y=1500, end_x=555, end_y=400, duration=800)


def login():
    driver.find_element(MobileBy.XPATH,
                        '//android.widget.TextView[@resource-id="com.ss.android.ugc.aweme:id/j5m" and @text="我"]').click()
    print("点击我")
    time.sleep(5)

    driver.find_element(MobileBy.XPATH,
                        '//android.widget.EditText[@resource-id="com.ss.android.ugc.aweme:id/gle"]').send_keys(
        '15081349872')
    print("输入手机号")
    time.sleep(5)
    driver.find_element(MobileBy.XPATH,
                        '//android.widget.LinearLayout[@resource-id="com.ss.android.ugc.aweme:id/czz"]/android.widget.LinearLayout').click()
    print("点击获取验证码")
    time.sleep(5)
    driver.find_element(MobileBy.XPATH, '//android.widget.Button[@resource-id="android:id/button2"]').click()
    print("点击取消")
    time.sleep(3)
    driver.find_element(MobileBy.XPATH, '//android.view.View[@resource-id="com.ss.android.ugc.aweme:id/xt"]').click()
    print("点击退出")


def search():
    # x=981 y=135 点击搜索按钮
    driver.tap([(981, 135)])
    print("点击搜索按钮")
    time.sleep(5)

    TouchAction(driver).tap(x=555, y=80).perform()
    time.sleep(5)

    # 等待输入框可点击
    WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable(
            (By.XPATH, '//android.widget.EditText[@resource-id="com.ss.android.ugc.aweme:id/aia"]'))
    ).send_keys('python')
    print("输入python")
    time.sleep(4)

    # # 定位并再次点击输入框，确保焦点仍在其中
    # search_box = driver.find_element(MobileBy.XPATH,
    #                                  '//android.widget.EditText[@resource-id="com.ss.android.ugc.aweme:id/aia"]')
    # search_box.click()
    # time.sleep(1)

    # os.system('adb shell input keyevent KEYCODE_ENTER')
    # print("通过 ADB 发送回车键")
def enter():
    # 模拟键盘的回车键
    driver.press_keycode(66)
    print("点击回车")
    TouchAction(driver).tap(x=970, y=1757).perform()
    time.sleep(5)


if __name__ == '__main__':
    caps = {
        "appActivity": ".main.MainActivity",
        "platformName": "Android",
        "platformVersion": "12",
        "deviceName": "127.0.0.1:16384",
        "appPackage": "com.ss.android.ugc.aweme"
    }

    url = "http://127.0.0.1:4723/wd/hub"

    driver = webdriver.Remote(url, caps)

    time.sleep(10)
    agree()
    swipe_onlyOne()
    search()
    enter()
    driver.quit()
